package com.blb.examsystem.dao.Impl;

import com.blb.common.entity.QuestionSet;
import com.blb.common.utils.DBUtils;
import com.blb.examsystem.dao.QuestionSetDao;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class QuestionSetDaoImpl implements QuestionSetDao {

    @Override
    public int selectCount() {
        return DBUtils.executeCount("select count(*) from t_question_set");
    }

    @Override
    public List<QuestionSet> selectLimit(int start, int length) {
        return DBUtils.executeQuery(QuestionSet.class,"select * from t_question_set limit ?,?",
                start,length);
    }

    @Override
    public int selectCountByTitle(String title) {
        return DBUtils.executeCount("select count(*) from t_question_set where title like ?",
                "%"+title+"%");
    }

    @Override
    public List<QuestionSet> selectLimitByTitle(String title, int start, int length) {
        return DBUtils.executeQuery(QuestionSet.class,"select * from t_question_set where title like ? limit ?,?",
                "%"+title+"%",start,length);
    }

    @Override
    public int insertQuestionSet(QuestionSet questionSet) {
        return DBUtils.executeUpdate("insert into t_question_set(title,description,subject_id,point,time) values(?,?,?,?,?)",
                questionSet.getTitle(),questionSet.getDescription(),questionSet.getSubjectId(),questionSet.getPoint(),
                questionSet.getTime());
    }

    @Override
    public List<QuestionSet> selectById(int id) {
        return DBUtils.executeQuery(QuestionSet.class,"select * from t_question_set where id=?",id);
    }

    @Override
    public int updateQuestionSet(QuestionSet questionSet) {
        return DBUtils.executeUpdate("update t_question_set set title=?,description=?,subject_id=?,point=?,time=? where id=?",
                questionSet.getTitle(),questionSet.getDescription(),questionSet.getSubjectId(),questionSet.getPoint(),
                questionSet.getTime(),questionSet.getId());
    }

    @Override
    public int deleteQuestionSet(int id) {
        return DBUtils.executeUpdate("delete from t_question_set where id=?",id);
    }
}
